<?php
namespace Api\Controller;
use Think\Controller;
class NotifyController extends Controller {
    public function index(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $map['out_trade_no'] = $out_trade_no;
        $data['transaction_id'] = $transaction_id;
        // $data['isPay'] = 1;
        $type = $_GET['type'];
        if($type == 'goods'){
            $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
            $data['is_pay'] = 1;
          //  $data['status'] = 1;
            $order = M("Order")->where($map)->find();
            $goods = M('goods')->where(['id'=>$order['goods_id']])->find();
            $shop = M('shop')->where(['shop_id'=>$order['shop_id']])->find();
            if($order['is_tuan']){
                $goods_id = $order['goods_id'];
                $eq4['is_pay'] = 1;
                $eq4['status'] = 0;
                $eq4['is_tuan'] = 1;
                $eq4['goods_id'] = $goods_id;
                $eq4['tuan_first_id'] = $order['tuan_first_id'];
                $eq4['is_tuan_first']=0;
                $tuan_order_count = M("Order")->where($eq4)->count();
                $data['tuan_pay_num'] = $tuan_order_count+2;
                //第一个拼团的
                if($order['is_tuan_first']){
                    $data['tuan_pay_num'] = $tuan_order_count+1;
                }
                if($order['tuan_need_num'] <= $data['tuan_pay_num'] && $order['tuan_need_num'] > 1){
                    $new['status'] = 1;
                    $new['tuan_pay_num'] = $data['tuan_pay_num'];
                    $eq['tuan_first_id'] = $order['tuan_first_id'];
                    //$eq['is_pay'] = 1;
                    $eq1['id'] = $order['tuan_first_id'];
                    M("Order")->where($eq)->save($new);
                    M("Order")->where($eq1)->save($new);
                    //发送团购成功通知
                    $tuaninfo = M()->query("select * from tp_order where is_pay =1 and tuan_first_id =".$order['tuan_first_id']." or id=".$order['tuan_first_id']);
                    //拼团用户
                    if($tuaninfo){
                        foreach($tuaninfo as $v){
                            sendTuanMessage($v['openid'],$v['out_trade_no'],$v['wid']);
                        }
                    }
                    //商家
                    $shop = M('shop')->where(['shop_id'=>$order['shop_id']])->find();
                    if($shop['openid']){
                        sendOrderMessage($shop['openid'],$out_trade_no,$order['wid'],'shop');//商家
                    }
                }else{
                    $data['status'] = 0;
                }
            }else{
                $data['status'] = 1;
            }
            if($order['is_kan']){
                //发送砍价成功商家通知
                if($shop['openid']){
                    sendOrderMessage($shop['openid'],$out_trade_no,$order['wid'],'shop');//商家
                }
            }
            elseif(!$order['is_kan'] && !$order['is_tuan'] && !$goods['is_qiang']){
                //普通订单支付成功通知
                sendOrderMessage($order['openid'],$out_trade_no,$order['wid'],'member');//用户
                if($shop['openid']){
                    sendOrderMessage($shop['openid'],$out_trade_no,$order['wid'],'shop');//商家
                }
            }
            elseif($goods['is_qiang']){
                //发送抢购通知
                sendQiangMessage($order['openid'],$out_trade_no,$order['wid']);//用户
                if($shop['openid']){
                    sendOrderMessage($shop['openid'],$out_trade_no,$order['wid'],'shop');//商家
                }
            }

            M("Order")->where($map)->save($data);
            //判断是否发佣金
            $wid = $order['wid'];
            $wechat = M('wechat')->where(['wid'=>$wid])->find();
                //商品参与分销，并且设置为支付时发放佣金
            if(($wechat['good_commission'] == 1) && $order['is_commission']){
                addGoodCommission($wid,4,$order['id'],$openid,$order['commission_one'],$order['commission_two'],$total_fee/100);
            }
            //购物产生积分
            //查出店铺设置
            $credit = $shop['credit_set'] *$total_fee/100;
            //判断有没有开通会员卡
            $card2 = M('shop_mycard')->where(['shop_id'=>$shop['shop_id'],'openid'=>$openid])->find();
            if($card2){
                M('shop_mycard')->where(['shop_id'=>$shop['shop_id'],'openid'=>$openid])->setInc('integral',$credit);
            }

        }elseif($type == 'gift'){
            $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
            $data['is_pay'] = 1;
            M("QuanGiftOrder")->where($map)->save($data);
        }elseif($type == 'shop'){
            $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
            $data['is_pay'] = 1;
            M("ShopOrder")->where($map)->save($data);
            $info = M("ShopOrder")->where($map)->find();
            $eq['shop_id'] = $info['shop_id'];
            $new['is_pay'] = 1;
            $new['status'] = 0;
            $new['join_end_time'] = date("Y-m-d H:i:s", strtotime("+ {$info['join_days']}days"));
            M("Shop")->where($eq)->save($new);
            //入驻发送模板消息，代理审核
            sendShenpiMessage($eq['shop_id'],'ruzhu');
            //商家入驻佣金
            $this->_addCommission($info['wid'], 0, $info['id'], $openid, 'comm_shopin_one', 'comm_shopin_two', $total_fee/100);
        }elseif($type == 'info'){
            $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
            $data['is_pay'] = 1;
            M("InfoOrder")->where($map)->save($data);
            $info = M("InfoOrder")->where($map)->find();
            $eq['id'] = $info['info_id'];
            if($info['top_days']>0){
                $new['is_top'] = 1;
                $new['top_end_time'] = date("Y-m-d H:i:s", strtotime("+ {$info['top_days']}days"));
                M("InfoOrder")->where($map)->save($new);
            }
            $new['status'] = 1;
            switch ($info['action']) {
                case 'job':
                    M("Job")->where($eq)->save($new);
                    break;
                case 'pinche':
                    M("Pinche")->where($eq)->save($new);
                    break;
                case 'house':
                    M("House")->where($eq)->save($new);
                    break;
                case 'info':
                    M("Info")->where($eq)->save($new);
                    break;
                default:

                    break;
            }
            //消息发布佣金
            $this->_addCommission($info['wid'], 2, $info['id'], $openid, 'comm_info_one', 'comm_info_two', $total_fee/100);
            //$this->_addCommission(2, 2, 2, "oRVj50L-3jTgEqslDeC7sz8lc-Ds", 'comm_info_one', 'comm_info_two', 10);
        //酒店预定支付
        }elseif($type == 'hotel'){
            //支付状态
            M("Order")->where($map)->save(['status' => 0, 'is_pay' => 1]);
        }
        echo "success";
    }
    public function renewShop(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $map['out_trade_no'] = $out_trade_no;
        $data['transaction_id'] = $transaction_id;

        $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
        $data['is_pay'] = 1;
        M("ShopOrder")->where($map)->save($data);
        M("ShopOrder")->where($map)->save($data);

        $info = M("ShopOrder")->where($map)->find();
        $eq['shop_id'] = $info['shop_id'];
        $new['is_pay'] = 1;
        $new['status'] = 1;
        $new['join_end_time'] = date("Y-m-d H:i:s", strtotime("+ {$info['join_days']}days"));
        M("Shop")->where($eq)->save($new);
    }
    public function reserveOrder(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $data = array();
        $data['is_pay'] = 1;
        $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
        $data['state'] = 1;
        M('takeout_ydorder')->where(array('ydcode'=>$out_trade_no))->save($data);

    }
    public function takeoutOrder(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $data = array();
        $data['is_pay'] = 1;
        $data['state'] = 2;
        $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
        M('takeout_order')->where(array('sh_ordernum'=>$out_trade_no))->save($data);
        //确认收货给店铺加钱及发放佣金
        $order = M('takeout_order')->where(array('sh_ordernum'=>$out_trade_no))->find();
        if($order['money']>0){
            $re2 =M('takeout_store')->where(['id'=>$order['seller_id']])->save(['takeout_money'=>$order['money']]);
            addDishesCommission($order['wid'],6,$order['id']);
        }

    }
    public function setTop(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $info_good = M('InfoOrder')->where(array('$out_trade_no'=>$out_trade_no))->find();
        if(!empty($info_good['top_end_time'])){
            $top_end_time = date("Y-m-d H:i:s",strtotime("+ {$info_good['top_days']}days"));
        }else{
            $top_end_time = date("Y-m-d", strtotime("+ {$info_good['top_days']}days", strtotime($info_good['top_end_time'])));
        }
        $data = array();
        $data['is_pay'] = 1;
        $data['pay_time'] = $top_end_time;
        $data['top_end_time'] = $top_end_time;
        M('InfoOrder')->where(array('out_trade_no'=>$out_trade_no))->save($data);

        $idata = array();
        $idata['is_top'] = 1;
        $idata['top_end_time'] = $top_end_time;
        M('info')->where(array('id'=>$info_good['info_id']))->save($idata);

    }
    //商家认领
    public function renLing(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $map['out_trade_no'] = $out_trade_no;
        $data['transaction_id'] = $transaction_id;

        $data['pay_time'] = date("Y-m-d H:i:s",strtotime($time_end));
        $data['is_pay'] = 1;
        M("ShopOrder")->where($map)->save($data);

        $info = M("ShopOrder")->where($map)->find();
        //支付成功修改认领状态，并修改店铺信息

//        M('renling')->where(['wid'=>$info['wid'],'openid'=>$info['openid']])->save(['status'=>0]);
//        $d['latitude']=I('latitude');
//        $d['longitude']=I('longitude');
//        $d['username'] = $info['username'];
//        $d['tel'] = $info['tel'];
//        $d['openid'] = $info['openid'];
//        $d['is_pay'] = 1;
//        $d['status'] = 2;
//        $d['join_end_time'] = date("Y-m-d H:i:s",strtotime("+".$info['join_days']." days",time()));
//        M('shop')->where(['wid'=>$info['wid'],'shop_id'=>$info['shop_id']])->save($d);

        $d['status'] = 0;
        $d['latitude']=I('latitude');
        $d['longitude']=I('longitude');
        $d['is_pay'] = 1;
        $d['join_days'] = $info['join_days'];
        M('renling')->where(['wid'=>$info['wid'],'openid'=>$info['openid']])->save($d);
        $id =M('renling')->where(['wid'=>$info['wid'],'openid'=>$info['openid']])->find();
        sendShenpiMessage($id['id'],'renling');
    }

    public function openCard(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $log = M('card_log')->where(['out_trade_no'=>$out_trade_no])->find();
        $new['is_pay'] = 1;
        $new['transaction_id'] = $transaction_id;
        M('card_log')->where(['id'=>$log['id']])->save($new);

        //佣金
        addCommission($log['wid'],1,$log['id'],$openid,'comm_tcard_one','comm_tcard_two',$total_fee/100);
    }
    //会员卡充值返回地址
    public function cardRecharge(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $log = M('shop_recharge_log')->where(['out_trade_no'=>$out_trade_no])->find();
        if($log['state'] == 0 ){
            $new['state'] = 1;
            $new['transaction_id'] = $transaction_id;
            M('shop_recharge_log')->where(['id'=>$log['id']])->save($new);
            //同时会员的账户增加
            $getMoney = $log['money'] + $log['get_money'];
            //获得积分
            $mycard = M('shop_mycard')->where(array('id'=>$log['mycard_id']))->find();
            $card = M('shop_card')->where(array('id'=>$mycard['card_id']))->find();
            //获得积分
            $getCredit = $log['money'] * $card['credit'];
            //会员账户增加积分和金额
            M('shop_mycard')->where(array('id'=>$log['mycard_id']))->setInc('money',$getMoney);
            //我的会员卡增加赠送的金额
            M('shop_mycard')->where(array('id'=>$log['mycard_id']))->setInc('get_money',$log['get_money']);
            //增加积分记录
            $data['wid'] = $log['wid'];
            $data['rechargelog_id'] = $log['id'];
            $data['mycard_id'] = $log['mycard_id'];
            $data['shop_id'] = $log['shop_id'];
            $data['openid'] = $log['openid'];
            $data['integral'] = $getCredit;
            $data['remark'] = '会员充值'.$log['money'].'获得积分';
            $data['create_time'] = date("Y-m-d H:i:s");
            //添加积分记录
            M('shop_integral_log')->add($data);

            M('shop_mycard')->where(array('id'=>$log['mycard_id']))->setInc('integral',$getCredit);
        }

        //佣金
        //addCommission($log['wid'],1,$log['id'],$openid,'comm_tcard_one','comm_tcard_two',$total_fee/100);
    }
//会员卡充次返回地址
    public function cardMeal(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $log = M('shop_meal_log')->where(['out_trade_no'=>$out_trade_no])->find();
        if($log['state'] == 0 ){
            $new['state'] = 1;
            $new['transaction_id'] = $transaction_id;
            M('shop_meal_log')->where(['id'=>$log['id']])->save($new);
            //获得积分
            $mycard = M('shop_mycard')->where(array('id'=>$log['mycard_id']))->find();
            $card = M('shop_card')->where(array('id'=>$mycard['card_id']))->find();
            //获得积分
            $getCredit = $log['money'] * $card['credit'];

            //增加积分记录
            $data['wid'] = $log['wid'];
            $data['rechargelog_id'] = $log['id'];
            $data['mycard_id'] = $log['mycard_id'];
            $data['shop_id'] = $log['shop_id'];
            $data['openid'] = $log['openid'];
            $data['integral'] = $getCredit;
            $data['remark'] = '会员充次'.$log['money'].'获得积分';
            $data['create_time'] = date("Y-m-d H:i:s");
            //添加积分记录
            M('shop_integral_log')->add($data);

            M('shop_mycard')->where(array('id'=>$log['mycard_id']))->setInc('integral',$getCredit);
        }

        //佣金
        //addCommission($log['wid'],1,$log['id'],$openid,'comm_tcard_one','comm_tcard_two',$total_fee/100);
    }

    public function levelup(){
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $log = M('level_order')->where(['out_trade_no'=>$out_trade_no])->find();

        $new['is_pay'] = 1;
        $new['pay_time'] = date('Y-m-d H:i:s',time());
        $new['transaction_id'] = $transaction_id;
        M('level_order')->where(['id'=>$log['id']])->save($new);
        //发送审核模版消息
        sendShenpiMessage($log['id'],'dianpu');
    }

    //申请成为团队队长
    public function team_leader()
    {
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $log = M('commission_team')->where(['out_trade_no'=>$out_trade_no])->find();

        $new['is_pay'] = 1;
        $new['transaction_id'] = $transaction_id;
        M('commission_team')->where(['id'=>$log['id']])->save($new);
        //发送模板消息
        sendShenpiMessage($log['id'],'leader');

    }
    //积分兑换商品时支付
    public function creditShop()
    {
        $xmlObj = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']); //解析回调数据
        $appid = (string)$xmlObj->appid;//微信appid
        $mch_id = (string)$xmlObj->mch_id;  //商户号
        $nonce_str = (string)$xmlObj->nonce_str;//随机字符串
        $sign = (string)$xmlObj->sign;//签名
        $result_code = (string)$xmlObj->result_code;//业务结果
        $openid = (string)$xmlObj->openid;//用户标识
        $is_subscribe = (string)$xmlObj->is_subscribe;//是否关注公众帐号
        $trace_type = (string)$xmlObj->trade_type;//交易类型，JSAPI,NATIVE,APP
        $bank_type = (string)$xmlObj->bank_type;//付款银行，银行类型采用字符串类型的银行标识。
        $total_fee = (string)$xmlObj->total_fee;//订单总金额，单位为分
        $fee_type = (string)$xmlObj->fee_type;//货币类型，符合ISO4217的标准三位字母代码，默认为人民币：CNY。
        $transaction_id = (string)$xmlObj->transaction_id;//微信支付订单号
        $out_trade_no = (string)$xmlObj->out_trade_no;//商户订单号
        $attach = (string)$xmlObj->attach;//商家数据包，原样返回
        $time_end = (string)$xmlObj->time_end;//支付完成时间
        $cash_fee = (string)$xmlObj->cash_fee;
        $return_code = (string)$xmlObj->return_code;

        $data = [
            'is_pay' => 1,
            'pay_time' => date('Y-m-d H:i:s', time()),
            'transaction_id' =>$transaction_id
        ];

        M('credit_order')->where(['out_trade_no'=>$out_trade_no])->save($data);

        //商品库存-1
        $order = M('credit_order')->where(['out_trade_no'=>$out_trade_no])->find();
        M('credit_goods')->where(['id'=>$order['goods_id']])->setDec('stock');
    }


    /*
     * 给上级添加佣金
     * @param $wid
     * @param $type 佣金来源：0店铺入驻 1同城卡 2信息发布 3店铺升级 4商品核销 5送礼物
     * @param $orderid 订单id
     * @param $openid
     * @param $field1 一级分销字段名称
     * @param $field2 二级分销字段名称
     * @param $price 订单金额(元)
     */
    private function _addCommission ($wid,$type,$orderid,$openid,$field1,$field2,$price) {
        //开启事务
        M()->startTrans();
        $level1 = [];
        $r = [[true]];
        //获取上级信息 两级
        $member = M('member')->where(['openid'=>$openid])->find();
        if(!$member){
            return false;
        }

        //团队分红
        $r[] = addTeamBonus($wid,$type,$orderid,$openid,$price);
        //$r1 = addTeamBonus($wid,$type,$orderid,$openid,$price);
        //获取后台设置信息。一级分销，还是二级分销
        $wechat = M('wechat')->where(['wid'=>$wid])->find();
        $commission_level = $wechat['commission_level'];

        //查佣金表，获取佣金设置
        $commission = M('commission_proportion')->where(['wid'=>$wid])->find();
        $commission1 = $commission[$field1];
        $commission2 =  $commission[$field2];
        if(!$commission){
            return false;
        }
        //一级
        if($member['agent']>0 && $commission1>0){
            $level1 = M('member')->where(['mid'=>$member['agent']])->find();
            $level1_info = getMember($wid,$level1['openid']);
            //判断是否同城卡是否可以使用，并且有上级
            if($level1 && $level1_info['has_card']){
                $data['wid'] = $wid;
                $data['money'] = $price*$commission1/100;
                $data['mid'] = $level1['mid'];
                $data['fmid'] = $member['mid'];//佣金来源mid
                $data['type'] = $type;
                $data['orderid'] = $orderid;
                $data['is_withdraw'] = 0;
                $data['time'] = date('Y-m-d H:i:s',time());
                $data['level'] =1;//几级
                $data['commission_proportion'] =$commission1; //佣金比例
                $r[] = M('commission')->add($data);
                //$r2 = M('commission')->add($data);
            }else{
                $r[] = true;
                //$r2 = true;
            }
        }
        //二级 查看设置是否开启二级分销
        if(($level1['agent']>0) && ($commission2>0) && ($commission_level>1)){
            $level2 = M('member')->where(['mid'=>$level1['agent']])->find();
            $level2_info = getMember($wid,$level2['openid']);
            //判断是否同城卡是否可以使用，并且有上级
            if($level2 && $level2_info['has_card']){
                $data2['wid'] = $wid;
                $data2['money'] = $price*$commission2/100;
                $data2['mid'] = $level2['mid'];
                $data2['fmid'] = $member['mid'];//佣金来源mid
                $data2['type'] = $type;
                $data2['orderid'] = $orderid;
                $data2['is_withdraw'] = 0;
                $data2['time'] = date('Y-m-d H:i:s',time());
                $data2['level'] =2;//几级
                $data2['commission_proportion'] =$commission2;//佣金比例
                $r[] = M('commission')->add($data2);
                //$r3 = M('commission')->add($data2);
            }else{
                $r[] = true;
                //$r3 = true;
            }
        }
        if(in_array(false,$r,true)){
            M()->rollback();
            return false;
        }else{
            M()->commit();
            return true;
        }
    }
}